<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Writes.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Model_DbTable_Admincontent extends Engine_Db_Table {
	
	protected $_serializedColumns = array ('params' );
	
	/**
	 * Set profile page default widget in admin content table without tab
	 *
	 * @param string $name
	 * @param int $page_id
	 * @param string $title
	 * @param int $titleCount
	 * @param int $order
	 */
	public function setAdminContentDefaultInfoWithoutTab($name = null, $page_id, $title = null, $titleCount = null, $order = null) {
		if (! empty ( $name )) {
			$select = $this->select ();
			$select_content = $select->from ( $this->info ( 'name' ) )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', $name )->limit ( 1 );
			$content = $select_content->query ()->fetchAll ();
			if (empty ( $content )) {
				$select = $this->select ();
				$select_container = $select->from ( $this->info ( 'name' ), array ('admincontent_id' ) )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
				$container = $select_container->query ()->fetchAll ();
				if (! empty ( $container )) {
					$select = $this->select ();
					$container_id = $container [0] ['admincontent_id'];
					$select_middle = $select->from ( $this->info ( 'name' ) )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
					$middle = $select_middle->query ()->fetchAll ();
					if (! empty ( $middle )) {
						$middle_id = $middle [0] ['admincontent_id'];
						$contentWidget = $this->createRow ();
						$contentWidget->page_id = $page_id;
						$contentWidget->type = 'widget';
						$contentWidget->name = $name;
						$contentWidget->parent_content_id = ($middle_id);
						$contentWidget->order = $order;
						$contentWidget->params = '{"title":"' . $title . '" , "titleCount":"' . $titleCount . '"}';
						$contentWidget->save ();
					}
				}
			}
		}
	}
	
	/**
	 * Set profile page default widget in admin content table
	 *
	 * @param string $name
	 * @param string $contentpage_id
	 * @param string $title
	 * @param string $titleCount
	 * @param string $order
	 */
	public function setAdminDefaultInfo($name = null, $page_id, $title = null, $titleCount = null, $order = null) {
		if (! empty ( $name )) {
			$select = $this->select ();
			$select_content = $select->from ( $this->info ( 'name' ) )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', $name )->limit ( 1 );
			$content = $select_content->query ()->fetchAll ();
			if (empty ( $content )) {
				$select = $this->select ();
				$select_container = $select->from ( $this->info ( 'name' ), array ('admincontent_id' ) )->where ( 'page_id = ?', $page_id )->where ( 'type = ?', 'container' )->limit ( 1 );
				$container = $select_container->query ()->fetchAll ();
				if (! empty ( $container )) {
					$container_id = $container [0] ['admincontent_id'];
					
					$select = $this->select ();
					$select_middle = $select->from ( $this->info ( 'name' ) )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
					$middle = $select_middle->query ()->fetchAll ();
					if (! empty ( $middle )) {
						$middle_id = $middle [0] ['admincontent_id'];
						
						$select = $this->select ();
						$select_tab = $select->from ( $this->info ( 'name' ) )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'page_id = ?', $page_id )->limit ( 1 );
						$tab = $select_tab->query ()->fetchAll ();
						if (! empty ( $tab )) {
							$tab_id = $tab [0] ['admincontent_id'];
						}
						$contentWidget = $this->createRow ();
						$contentWidget->page_id = $page_id;
						$contentWidget->type = 'widget';
						$contentWidget->name = $name;
						$contentWidget->parent_content_id = ($tab_id ? $tab_id : $middle_id);
						$contentWidget->order = $order;
						$contentWidget->params = '{"title":"' . $title . '" , "titleCount":"' . $titleCount . '"}';
						$contentWidget->save ();
					}
				}
			}
		}
	}

}

?>